//编辑职业意向
import React, { Component } from 'react'
import {
    View,
    Text,
    TextInput,
    TouchableOpacity,
} from 'react-native';

import { posedit_Style as styles } from '../../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';
var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;
var height = Dimensions.get('window').height;

export default class Posedit_body extends Component {
    constructor(props) {
        super(props);
        this.state = {
            resume: {},
            intentMon: 12,
            mon: 12,
            yearIntentSal: 0,//期望年薪
            yearSal: 0,//当前年薪
        };
    };

    //加载后渲染页面
    componentDidMount() {
        if (!this.state.resume.resumeId && this.props.resume.resumeId) {
            this.setState({ resume: this.props.resume });
            this.resumeIntentMm(this.props.resume.resumeIntentMm);
            this.resumeMm(this.props.resume.resumeMm);
        }
    }

    componentDidUpdate() {
        if (!this.state.resume.resumeId && this.props.resume.resumeId) {
            this.setState({ resume: this.props.resume });
        }
    }

    render() {
        return (
            <View style={{ flex: 1, backgroundColor: '#ffffff' }}>
                <View style={[styles.rtopin, {
                    width: width * 1,
                    elevation: 5,
                    shadowColor: 'black',
                    shadowOpacity: 1,
                    shadowRadius: 5
                }]}>

                    <TouchableOpacity activeOpacity={1} style={styles.rtopicon1} onPress={() => { this.props.navigation.goBack() }}>
                        <Icon
                            name={'ios-arrow-back'}
                            size={25}
                            color={'#ffffff'} />
                    </TouchableOpacity>
                    <Text style={styles.rtoptext}>职业意向</Text>
                    <TouchableOpacity style={styles.rtoptextout} activeOpacity={1}
                        onPress={() => {
                            const r = {
                                resumeId: this.state.resume.resumeId,
                                memberId: this.state.resume.memberId,
                                fields: this.state.resume.fields,
                                resumeIntentPosition: this.state.resume.resumeIntentPosition,
                                resumeIntentWorkspace: this.state.resume.resumeIntentWorkspace,
                                resumeIntentMm: this.state.resume.resumeIntentMm,
                                resumeMm: this.state.resume.resumeMm,
                            }
                            this.props.actions.UpdateResumeAction(r);
                            this.props.navigation.goBack();
                        }}>
                        <Text style={styles.rtoptext2}>保存</Text>
                    </TouchableOpacity>
                </View >

                <View style={styles.rout}>
                    {/* 期望行业 */}
                    <View style={styles.rmall}>
                        <Text style={styles.rtxt}>期望行业</Text>
                        <TouchableOpacity style={[styles.rchooseout, { width: width * 0.7 }]} activeOpacity={1}
                            onPress={() => {
                                this.props.navigation.navigate('Trade',
                                    { Type: 3, 'rollback': this.setHy.bind(this), list: this.state.resume.fields })
                            }}>
                            <Text style={styles.rchoose}>
                                {this.fieldName(this.state.resume.fields, 3)}
                            </Text>
                        </TouchableOpacity>
                    </View>

                    {/* 期望职位 */}
                    <View style={styles.rmall}>
                        <Text style={styles.rtxt}>期望职能</Text>
                        <TextInput
                            style={[styles.inputname, { width: width * 0.7 }]}
                            underlineColorAndroid="transparent"
                            defaultValue={this.state.resume.resumeIntentPosition}
                            onChangeText={this.resumeIntentPosition.bind(this)}
                        />
                    </View>

                    {/* 期望地点 */}
                    <View style={styles.rmall}>
                        <Text style={styles.rtxt}>期望地点</Text>
                        <TouchableOpacity style={[styles.rchooseout, { width: width * 0.7 }]} activeOpacity={1}
                            onPress={() => {
                                this.props.navigation.navigate('cityList',
                                    {
                                        'callback': this.resumeIntentWorkspace.bind(this)
                                    });
                            }}>
                            <Text style={styles.rchoose}>{this.state.resume.resumeIntentWorkspace}</Text>
                        </TouchableOpacity>
                    </View>

                    {/* 期望年薪 */}
                    <View style={styles.rmall2}>
                        <Text style={styles.rtxt}>期望年薪</Text>
                        <TextInput
                            style={[styles.inputname2, { width: width * 0.2 }]}
                            underlineColorAndroid="transparent"
                            keyboardType='numeric'
                            defaultValue={this.NfotS(this.state.resume.resumeIntentMm)}
                            onChangeText={this.resumeIntentMm.bind(this)}
                        />
                        <Text style={styles.rtxt}>元</Text>
                        <Text style={styles.rtxt2}>×</Text>
                        <Text style={styles.rtxt}>12个月</Text>
                        <Text style={styles.rtxt2}>=</Text>
                        <Text style={styles.rtxt}>{this.state.yearIntentSal}万</Text>
                    </View>

                    {/* 目前年薪 */}
                    <View style={styles.rmall2}>
                        <Text style={styles.rtxt}>目前年薪</Text>
                        <TextInput
                            style={[styles.inputname2, { width: width * 0.2 }]}
                            underlineColorAndroid="transparent"
                            keyboardType='numeric'
                            defaultValue={this.NfotS(this.state.resume.resumeMm)}
                            onChangeText={this.resumeMm.bind(this)}
                        />
                        <Text style={styles.rtxt}>元</Text>
                        <Text style={styles.rtxt2}>×</Text>
                        <Text style={styles.rtxt}>12个月</Text>
                        <Text style={styles.rtxt2}>=</Text>
                        <Text style={styles.rtxt}>{this.state.yearSal}万</Text>
                    </View>
                </View >
            </View>
        )
    }

    setHy(val, type) {
        for (let i = 0; i < this.state.resume.fields.length; i++) {
            if (this.state.resume.fields[i].fieldType == type) {
                this.state.resume.fields.splice(i, 1);
                i = -1;
            }
        }
        const resume = this.state.resume;
        resume.fields = resume.fields.concat(val);
        this.setState({
            resume: resume
        })
    }

    //经营领域
    fieldName(List, sl) {
        let arr = '';
        if (List != undefined) {
            let s = 1;
            let length = List.length;
            for (let i = 0; i < List.length; i++) {
                if (List[i].fieldType == '3' && s <= sl) {
                    arr += List[i].fieldName + '/';
                    ++s;
                }
            }
            if (arr.endsWith('/')) {
                arr = arr.substr(0, arr.length - 1);
            }
        }
        return arr;
    }

    //期望职能
    resumeIntentPosition(a) {
        const resume = this.state.resume;
        resume.resumeIntentPosition = a;
        this.setState({ resume: resume });
    }

    //当前城市
    resumeIntentWorkspace(a) {
        const resume = this.state.resume;
        resume.resumeIntentWorkspace = a;
        this.setState({ resume: resume });
    }

    //期望年薪
    resumeIntentMm(a) {
        const resume = this.state.resume;
        if (isNaN(a) || a === '' || a === null) {
            resume.resumeIntentMm = 0;
        } else {
            resume.resumeIntentMm = a;
        }
        this.setState({
            resume: resume,
            yearIntentSal: a * parseInt(12) / 10000
        });
    }

    //当前年薪
    resumeMm(a) {
        const resume = this.state.resume;
        if (isNaN(a) || a == '') {
            resume.resumeMm = 0;
        } else {
            resume.resumeMm = a;
        }
        this.setState({
            resume: resume,
            yearSal: a * parseInt(12) / 10000
        });
    }

    NfotS(num) {
        let s = '';
        if (num != null) {
            s += num + '';
        }else{
            s='0';
        }
        return s;
    }

}